package com.eduagent.xwqeduagent.api.InstantVoiceToTextConversion.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;

import java.util.List;

/**
 * 讯飞语音识别结果模型
 */
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class RecognitionResult {
    private int code;
    private String message;
    private String sid;
    private Data data;

    @lombok.Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Data {
        private int status;
        private Result result;
    }

    @lombok.Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Result {
        private int bg;
        private int ed;
        private boolean ls;
        private String pgs;
        private int sn;
        private List<Integer> rg;
        private List<Word> ws;
        private Vad vad;
    }

    @lombok.Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Word {
        private int bg;
        private List<Cw> cw;
    }

    @lombok.Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Cw {
        private double sc;
        private String w;
    }

    @lombok.Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Vad {
        private List<VadInfo> ws;
    }

    @lombok.Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class VadInfo {
        private int bg;
        private int ed;
        private double eg;
    }

    /**
     * 解析识别结果文本
     * 
     * @return 识别到的文本
     */
    public String getRecognizedText() {
        if (data == null || data.result == null || data.result.ws == null) {
            return "";
        }
        
        StringBuilder text = new StringBuilder();
        for (Word word : data.result.ws) {
            for (Cw cw : word.cw) {
                text.append(cw.w);
            }
        }
        return text.toString();
    }
    
    /**
     * 判断是否是动态修正结果
     * 
     * @return 是否为动态修正结果
     */
    public boolean isDynamicCorrection() {
        if (data == null || data.result == null) {
            return false;
        }
        return "rpl".equals(data.result.pgs);
    }
    
    /**
     * 获取替换范围
     * 
     * @return 替换范围数组[开始位置,结束位置]
     */
    public int[] getReplaceRange() {
        if (data == null || data.result == null || data.result.rg == null || data.result.rg.size() < 2) {
            return new int[]{0, 0};
        }
        return new int[]{data.result.rg.get(0), data.result.rg.get(1)};
    }
    
    /**
     * 判断是否是最后一个结果
     * 
     * @return 是否为最后一个结果
     */
    public boolean isLastResult() {
        return data != null && data.status == 2;
    }
} 